package nl.scalasim
package actors
package citizenactivities

import nl.scalasim.actors.Citizen
import nl.scalasim.util.Accumulator

/**
 * Activity for a citizen at home.
 *
 * @author Yigal Duppen
 * @since Oct 28, 2010
 */
class AtHomeActivity(val citizen: Citizen) extends Activity {

  private var accumulator: Accumulator = _

  def activate = {
    accumulator = Accumulator(citizen.ticksAtHome)
  }

  def handleAct = {
    accumulator = accumulator.accumulate

    accumulator.overflow match {
      case 1 if citizen.workplace != None => {
        goToWork
      }

      case _ => {
        log.info(citizen + " is doing at home stuff")
        done()
      }
    }
  }

  private def goToWork: Unit = {
    log.info("Free time is over, moving to " + citizen.workplace.get)
    citizen.currentActivity = citizen.commuting
    citizen.commuting.nextActivity = citizen.atWork
    citizen.addressBook.choreographer ! MoveTo(citizen, citizen.workplace.get)
    done()
  }

}


